function crypto_call_back(event, mode, back) {
    window.crypto.subtle.digest(
        {
            name: mode,
        },
        event.target.result
    )
        .then(function (hash) {
            let hexString = '', hashResult = new Uint8Array(hash);
            for (let i = 0; i < hashResult.length; i++) {
                hexString += ("00" + hashResult[i].toString(16)).slice(-2);
            }
            back({code: 0, str: hexString.toUpperCase()});
        })
        .catch(function (err) {
            console.log(err);
            back({code: 1, str: err});
        });
}

function to_crypto(event, back, mode) {
    let files = event.dataTransfer ? event.dataTransfer.files : event.target.files;
    let reader = new FileReader();
    reader.onload = function (event) {
        crypto_call_back(event, mode, back);
    };
    reader.readAsArrayBuffer(files[0]);
}
function init_hash() {
    if (!window.File || !window.FileReader || !window.FileList || !window.Blob || !File.prototype.slice) {
        alert('File APIs are not fully supported in this browser. Please use latest Mozilla Firefox or Google Chrome.');
    }
    if ((typeof File !== 'undefined') && !File.prototype.slice) {
        if (File.prototype.webkitSlice) {
            File.prototype.slice = File.prototype.webkitSlice;
        }

        if (File.prototype.mozSlice) {
            File.prototype.slice = File.prototype.mozSlice;
        }
    }
}
// function getMd5(input_id, callback) {
//     init_hash();
//     document.getElementById(input_id).addEventListener('change', function (event) {
//         to_crypto(event, callback, "MD5");
//     }, false);
// }
function getHash(input_id, callback) {
    init_hash();
    document.getElementById(input_id).addEventListener('change', function (event) {
        to_crypto(event, callback, "SHA-256");
    }, false);
}